<template>
  <div class="app-container">
    <SearchFilter
      size="medium"
      :maxShow="3"
      @search="handleQuery"
      @reset="resetQuery"
    >
      <el-form
        :model="queryParams"
        label-position="right"
        label-width="70px"
        ref="queryForm"
      >
        <el-row :gutter="20">
          <el-col :span="7">
            <el-form-item label="项目名称" prop="projectName">
              <el-input
                v-model="queryParams.projectName"
                placeholder="请输入"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="负责人" prop="projectManager">
              <el-input
                v-model="queryParams.projectManager"
                placeholder="请输入"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="业主单位" prop="ownerUnit">
              <el-input
                v-model="queryParams.ownerUnit"
                placeholder="请输入"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </SearchFilter>

    <el-row :gutter="20">
      <el-col :span="5">
        <div class="catalog-wrapper">
          <div class="input-box">
            <el-input
              placeholder="输入关键字搜索"
              v-model="filterText"
              class="search"
            ></el-input>
            <el-button
              type="info"
              plain
              icon="el-icon-sort"
              size="mini"
              class="toggle-expand"
              @click="toggleExpandAll"
            ></el-button>
          </div>

          <el-tree
            :data="catalogTreeData"
            node-key="id"
            ref="catalogTree"
            @node-click="nodeclick"
            :default-expand-all="isExpandAll"
            :filter-node-method="filterNode"
            :highlight-current="true"
            class="catalog-tree"
            v-if="refreshTree"
          ></el-tree>
        </div>
      </el-col>
      <el-col :span="19">
        <div class="content-container">
          <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
            <el-col :span="1.5">
              <p class="header-title">项目列表</p>
            </el-col>
            <div>
              <el-col :span="1.5" v-if="ids.length">
                <el-button
                  size="small"
                  v-hasPermi="['resource:project:remove']"
                  @click="handleDelete"
                  >批量删除</el-button
                >
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  icon="el-icon-plus"
                  size="small"
                  @click="handleAdd"
                  v-hasPermi="['resource:project:add']"
                  >新增项目</el-button
                >
              </el-col>
              <el-col :span="1.5">
                <template-btn-group></template-btn-group>
              </el-col>
            </div>
          </el-row>

          <el-table
            v-loading="loading"
            :data="projectList"
            @selection-change="handleSelectionChange"
            style="minwidth: 700px"
          >
            <el-table-column type="selection" min-width="55" align="center" />
            <el-table-column
              type="index"
              label="序号"
              align="center"
              min-width="120"
            >
              <template slot-scope="scope">
                <span>{{
                  (queryParams.pageNum - 1) * queryParams.pageSize +
                  scope.$index +
                  1
                }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="项目名称"
              align="center"
              min-width="120"
              prop="projectName"
            />
            <el-table-column
              label="项目图片"
              align="center"
              min-width="120"
              prop="img"
            >
              <template slot-scope="scope">
                <img
                  :src="scope.row.img"
                  alt=""
                  v-if="scope.row.img"
                  class="image"
                />
                <p class="image" v-else></p>
              </template>
            </el-table-column>
            <el-table-column
              label="业主单位"
              align="center"
              min-width="120"
              prop="ownerUnit"
            />
            <el-table-column
              label="业主联系人"
              align="center"
              min-width="120"
              prop="owner"
            />
            <el-table-column
              label="联系电话"
              align="center"
              min-width="120"
              prop="ownerPhone"
            />
            <el-table-column
              label="项目经理"
              align="center"
              min-width="120"
              prop="projectManager"
            />
            <!-- <el-table-column
              label="合同时间"
              align="center"
              min-width="120"
              prop="contractDate"
            />
            <el-table-column
              label="到期时间"
              align="center"
              min-width="120"
              prop="contractEndDate"
            /> -->
            <el-table-column
              label="质保时间"
              align="center"
              min-width="120"
              prop="warrantyTime"
            />
            <el-table-column
              label="项目类型"
              align="center"
              min-width="120"
              prop="projectType"
            >
              <template slot-scope="scope">
                <dict-tag
                  :options="dict.type.project_type"
                  :value="scope.row.projectType"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="备注"
              align="center"
              min-width="120"
              prop="remark"
            />
            <el-table-column
              label="操作"
              align="center"
              fixed="right"
              width="180"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  @click="getRouterInfo(scope.row)"
                  v-hasPermi="['resource:project:edit']"
                  >查看</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['resource:project:edit']"
                  >修改</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  @click="handleDelete(scope.row)"
                  class="text-danger"
                  v-hasPermi="['resource:project:remove']"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </el-col>
    </el-row>

    <!-- 添加或修改项目信息对话框 -->
    <el-dialog
      :title="title"
      custom-class="dialog"
      :visible.sync="open"
      append-to-body
      :close-on-click-modal="false"
      width="740px"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-position="top"
        label-width="80px"
      >
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="项目名称" prop="projectName">
              <el-input
                v-model="form.projectName"
                placeholder="请输入项目名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="业主单位" prop="ownerUnit">
              <el-input v-model="form.ownerUnit" placeholder="请输入业主单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="单位地址" prop="ownerUnitArea">
              <el-input
                v-model="form.ownerUnitArea"
                placeholder="请输入单位地址"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="业主负责人" prop="owner">
              <el-input v-model="form.owner" placeholder="请输入业主负责人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="业主负责人电话" prop="ownerPhone">
              <el-input
                v-model="form.ownerPhone"
                placeholder="请输入业主负责人电话"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <!-- <el-form-item label="合同时间" prop="contractDate">
              <el-date-picker
                v-model="form.contractDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item> -->
            <el-form-item label="使用单位" prop="useUnit">
              <el-input v-model="form.useUnit" placeholder="请输入使用单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="使用单位联系人" prop="useName">
              <el-input
                v-model="form.useName"
                placeholder="请输入使用单位联系人"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="使用单位电话" prop="usePhone">
              <el-input
                v-model="form.usePhone"
                placeholder="请输入使用单位电话"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="设计单位" prop="designUnit">
              <el-input
                v-model="form.designUnit"
                placeholder="请输入设计单位"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="设计单位联系人" prop="designName">
              <el-input
                v-model="form.designName"
                placeholder="请输入设计单位联系人"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="设计单位电话" prop="designPhone">
              <el-input
                v-model="form.designPhone"
                placeholder="请输入设计单位电话"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="监理单位" prop="superviseUnit">
              <el-input
                v-model="form.superviseUnit"
                placeholder="请输入监理单位"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="监理单位联系人" prop="superviseName">
              <el-input
                v-model="form.superviseName"
                placeholder="请输入监理单位联系人"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="监理单位电话" prop="supervisePhone">
              <el-input
                v-model="form.supervisePhone"
                placeholder="请输入监理单位电话"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11"
            ><el-form-item label="总包单位" prop="contractingUnit">
              <el-input
                v-model="form.contractingUnit"
                placeholder="请输入总包单位"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="总包单位联系人" prop="contractingName">
              <el-input
                v-model="form.contractingName"
                placeholder="请输入总包单位联系人"
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="总包单位电话" prop="contractingPhone">
              <el-input
                v-model="form.contractingPhone"
                placeholder="请输入总包单位电话"
              /> </el-form-item
          ></el-col>
          <el-col :span="11"
            ><el-form-item label="合作单位" prop="cooperateUnit">
              <el-input
                v-model="form.cooperateUnit"
                placeholder="请输入合作单位"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11"
            ><el-form-item label="合作单位联系人" prop="cooperateName">
              <el-input
                v-model="form.cooperateName"
                placeholder="请输入合作单位联系人"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="合作单位电话" prop="cooperatePhone">
              <el-input
                v-model="form.cooperatePhone"
                placeholder="请输入合作单位电话"
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="跟审单位" prop="checkUnit">
              <el-input
                v-model="form.checkUnit"
                placeholder="请输入跟审单位"
              /> </el-form-item
          ></el-col>
          <el-col :span="11"
            ><el-form-item label="跟审单位联系人" prop="checkName">
              <el-input
                v-model="form.checkName"
                placeholder="请输入跟审单位联系人"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11"
            ><el-form-item label="跟审单位电话" prop="checkPhone">
              <el-input
                v-model="form.checkPhone"
                placeholder="请输入跟审单位电话"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="预算审核单位" prop="budgetReviewUnit">
              <el-input
                v-model="form.budgetReviewUnit"
                placeholder="请输入预算审核单位"
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11"
            ><el-form-item label="预算审核联系人" prop="budgetReviewName">
              <el-input
                v-model="form.budgetReviewName"
                placeholder="请输入预算审核联系人"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11"
            ><el-form-item label="预算审核电话" prop="budgetReviewPhone">
              <el-input
                v-model="form.budgetReviewPhone"
                placeholder="请输入预算审核电话"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11"
            ><el-form-item label="审计单位" prop="auditUnit">
              <el-input v-model="form.auditUnit" placeholder="请输入审计单位" />
            </el-form-item>
          </el-col>
          <el-col :span="11"
            ><el-form-item label="审计单位联系人" prop="auditName">
              <el-input
                v-model="form.auditName"
                placeholder="请输入审计单位联系人"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="审计单位电话" prop="auditPhone">
              <el-input
                v-model="form.auditPhone"
                placeholder="请输入审计单位电话"
              /> </el-form-item
          ></el-col>
          <el-col :span="11"
            ><el-form-item label="检测单位" prop="testUnit">
              <el-input v-model="form.testUnit" placeholder="请输入检测单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="检测单位联系人" prop="testName">
              <el-input
                v-model="form.testName"
                placeholder="请输入检测单位联系人"
              /> </el-form-item
          ></el-col>
          <el-col :span="11">
            <el-form-item label="检测单位电话" prop="testPhone">
              <el-input
                v-model="form.testPhone"
                placeholder="请输入检测单位电话"
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="运营商" prop="operators">
              <el-input
                v-model="form.operators"
                placeholder="请输入运营商"
              /> </el-form-item
          ></el-col>
          <el-col :span="11">
            <el-form-item label="运营商开通时间" prop="operatorsTime">
              <el-date-picker
                clearable
                v-model="form.operatorsTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择运营商开通时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="运营商电话" prop="operatorsPhone">
              <el-input
                v-model="form.operatorsPhone"
                placeholder="请输入运营商电话"
              /> </el-form-item
          ></el-col>
          <el-col :span="11"
            ><el-form-item label="分包单位" prop="subcontractUnit">
              <el-input
                v-model="form.subcontractUnit"
                placeholder="请输入分包单位"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="分包单位联系人" prop="subcontractName">
              <el-input
                v-model="form.subcontractName"
                placeholder="请输入分包单位联系人"
              /> </el-form-item
          ></el-col>
          <el-col :span="11">
            <el-form-item label="分包单位电话" prop="subcontractPhone">
              <el-input
                v-model="form.subcontractPhone"
                placeholder="请输入分包单位电话"
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11"
            ><el-form-item label="业务负责人" prop="businessManager">
              <el-input
                v-model="form.businessManager"
                placeholder="请输入业务负责人"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="业务负责人电话" prop="businessPhone">
              <el-input
                v-model="form.businessPhone"
                placeholder="请输入业务负责人电话"
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11"
            ><el-form-item label="售前负责人" prop="presaleManager">
              <el-input
                v-model="form.presaleManager"
                placeholder="请输入售前负责人"
              /> </el-form-item
          ></el-col>
          <el-col :span="11">
            <el-form-item label="售前负责人电话" prop="presalePhone">
              <el-input
                v-model="form.presalePhone"
                placeholder="请输入售前负责人电话"
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="分包单位负责人" prop="subcontractManager">
              <el-input
                v-model="form.subcontractManager"
                placeholder="请输入分包单位负责人"
              /> </el-form-item
          ></el-col>
          <el-col :span="11"
            ><el-form-item
              label="分包单位负责人电话"
              prop="subcontractManagerPhone"
            >
              <el-input
                v-model="form.subcontractManagerPhone"
                placeholder="请输入分包单位负责人电话"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item
              label="总包预估工期"
              prop="contractingPredictDuration"
            >
              <el-input
                v-model="form.contractingPredictDuration"
                placeholder="请输入总包预估工期"
              /> </el-form-item
          ></el-col>
          <el-col :span="11">
            <el-form-item label="总包实际工期" prop="contractingActualDuration">
              <el-input
                v-model="form.contractingActualDuration"
                placeholder="请输入总包实际工期"
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11"
            ><el-form-item label="项目工期" prop="projectDuration">
              <el-input
                v-model="form.projectDuration"
                placeholder="请输入项目工期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="项目实际工期" prop="actualDuration">
              <el-input
                v-model="form.actualDuration"
                placeholder="请输入项目实际工期"
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="开工日期" prop="startDate">
              <el-date-picker
                clearable
                v-model="form.startDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择开工日期"
              >
              </el-date-picker> </el-form-item
          ></el-col>
          <el-col :span="11">
            <el-form-item label="初验日期" prop="inspectDate">
              <el-date-picker
                clearable
                v-model="form.inspectDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择初验日期"
              >
              </el-date-picker> </el-form-item
          ></el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="竣工日期" prop="completionDate">
              <el-date-picker
                clearable
                v-model="form.completionDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择竣工日期"
              >
              </el-date-picker> </el-form-item
          ></el-col>
          <el-col :span="11"
            ><el-form-item label="质保日期" prop="warrantyTime">
              <!-- <el-input
                v-model="form.warrantyTime"
                placeholder="请输入质保时间"
              /> -->
              <el-date-picker
                clearable
                v-model="form.warrantyTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择质保日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="项目经理" prop="projectManager">
              <!-- <el-select
                v-model="form.projectManager"
                placeholder="请输入项目经理"
                @change="managerChange"
              >
                <el-option
                  v-for="item in managerList"
                  :key="item.nickName"
                  :label="item.nickName"
                  :value="item.nickName"
                >
                </el-option> 
              </el-select>-->
              <el-input
                v-model="form.projectManager"
                placeholder="请输入项目经理"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="项目经理电话" prop="projectManagerPhone">
              <el-input
                v-model="form.projectManagerPhone"
                placeholder="请输入项目经理电话"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <!-- <el-col :span="11">
            <el-form-item label="到期时间" prop="contractEndDate">
              <el-date-picker
                v-model="form.contractEndDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col> -->

          <el-col :span="11">
            <el-form-item label="关联目录" prop="catId">
              <el-cascader
                v-model="catalogValue"
                :options="catalogTreeData"
                @change="handleChange"
                :props="{ value: 'id' }"
                style="width: 100%"
                clearable
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="项目类型" prop="projectType">
              <el-select
                v-model="form.projectType"
                placeholder="请选择项目类型"
              >
                <el-option
                  v-for="item in dict.type.project_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex" justify="space-between">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                type="textarea"
                v-model="form.remark"
                placeholder="请输入备注"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="24">
            <el-form-item label="项目图片" prop="img">
              <image-upload v-model="form.img" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="24">
            <el-form-item label="上传附件" prop="file">
              <file-upload v-model="form.file" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-col :span="24">
            <el-form-item label="上传清单" prop="deviceFile">
              <device-upload
                :projectType="form.projectType"
                v-model="form.deviceFile"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 关联设备 -->
    <device-dialog
      v-if="deviceDialogVisible"
      ref="deviceDialog"
    ></device-dialog>
  </div>
</template>

<script>
import {
  listProject,
  getProject,
  delProject,
  addProject,
  updateProject,
} from "@/api/resource/project";
import { getTreeStruct } from "@/api";
import { getUserList } from "@/api/menu";
import SearchFilter from "@/components/SearchFile";
import DeviceDialog from "./DeviceDialog.vue";
import TemplateBtnGroup from "./TemplateBtnGroup.vue";
import DeviceUpload from "./DeviceUpload.vue";

export default {
  name: "Project",
  components: { SearchFilter, DeviceDialog, TemplateBtnGroup, DeviceUpload },
  dicts: ["project_type"],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目信息表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaName: null,
        areaImg: null,
        projectId: null,
        principal: null,
        projectName: null,
        projectManager: null,
        projectManagerPhone: null,
        file: null,
      },
      // 表单参数
      form: {
        projectName: "",
        ownerUnit: "",
        ownerUnitArea: "",
        owner: "",
        ownerPhone: "",
        contractDate: "",
        contractEndDate: "",
        projectManager: "",
        projectManagerPhone: "",
        remark: "",
        img: "",
        file: "",
      },
      // 表单校验
      rules: {
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" },
        ],
        ownerUnit: [
          { required: true, message: "业主单位不能为空", trigger: "blur" },
        ],
        ownerUnitArea: [
          { required: true, message: "单位地址不能为空", trigger: "blur" },
        ],
        owner: [
          { required: true, message: "业主负责人不能为空", trigger: "blur" },
        ],
        ownerPhone: [
          {
            required: true,
            message: "业主负责人电话不能为空",
            trigger: "blur",
          },
          {
            // pattern: /(1[34578]\d{9})$/,
            pattern: /^(1[3456789]\d{9}|0\d{2,3}-\d{7,8})$/,
            message: "请填写正确的手机号码",
            trigger: "change",
          },
        ],
        contractDate: [
          { required: true, message: "合同时间不能为空", trigger: "blur" },
        ],
        contractEndDate: [
          { required: true, message: "到期时间不能为空", trigger: "blur" },
        ],
        projectManager: [
          { required: true, message: "项目经理不能为空", trigger: "blur" },
        ],
        projectManagerPhone: [
          { required: true, message: "项目经理电话不能为空", trigger: "blur" },
          {
            pattern: /^(1[3456789]\d{9}|0\d{2,3}-\d{7,8})$/,
            message: "请填写正确的手机号码",
            trigger: "change",
          },
        ],
        projectType: [
          { required: true, message: "项目类型不能为空", trigger: "blur" },
        ],
      },
      managerList: [],
      filterText: "",
      catalogTreeData: [],
      catalogValue: [],
      catId: null,
      refreshTree: true,
      isExpandAll: false,
      deviceDialogVisible: false,
    };
  },
  created() {
    this.getList();
    // this.getUserListHandle();
    this.getCatalogTree();
  },
  methods: {
    async getUserListHandle() {
      const deptCode = this.$store.state.user.user.dept.deptCode;
      const roleKey = deptCode.substring(deptCode.length - 4);
      const res = await getUserList({ roleKey: roleKey + "-manager" });
      if (res.data.length) {
        this.managerList = res.data.map((item) => {
          return { ...item, userId: item.userId + "" };
        });
      }
    },
    managerChange(e) {
      let obj = this.managerList.find((item) => item.nickName === e);
      this.form.projectManagerPhone = obj.phonenumber;
    },
    /** 查询项目信息列表 */
    getList() {
      this.loading = true;
      listProject({
        ...this.queryParams,
        deptId: this.$store.state.user.user.deptId,
        catId: this.catId,
      }).then((response) => {
        response.rows.forEach((item) => {
          if (item.img) {
            item.img = this.baseUrl + item.img;
          }
        });
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        areaName: null,
        areaImg: null,
        projectId: null,
        createTime: null,
        updateTime: null,
        principal: null,
        principalPhone: null,
        projectManager: null,
        projectManagerPhone: null,
        remark: null,
        file: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.catId = null;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProject(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目信息";
        this.catalogValue = response.data.catalogPath;
      });
    },
    /** 跳转详情 */
    getRouterInfo({ id }) {
      this.$router.push("/resource/projectQuery/" + id);
    },
    // 设备
    handleDevice(row) {
      const { id } = row;
      this.deviceDialogVisible = true;
      this.$nextTick(() => {
        this.$refs.deviceDialog.init(id);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        console.log(this.form);
        if (valid) {
          if (this.form.id != null) {
            updateProject({
              ...this.form,
              deptId: this.$store.state.user.user.deptId,
            }).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProject({
              ...this.form,
              deptId: this.$store.state.user.user.deptId,
            }).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除项目信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delProject(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "resource/project/export",
        {
          ...this.queryParams,
        },
        `project_${new Date().getTime()}.xlsx`
      );
    },
    getCatalogTree() {
      this.catalogTreeData = [];
      getTreeStruct().then((res) => {
        this.catalogTreeData = res.data;
      });
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    nodeclick(data, node, component) {
      console.log("节点被点击", data, node, component);
      if (node.level) {
        this.catId = data.id;
        this.getList();
      }
    },
    handleChange() {
      console.log(this.catalogValue);
      this.form.catId = this.catalogValue[this.catalogValue.length - 1];
      console.log(this.form);
    },
    toggleExpandAll() {
      this.refreshTree = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTree = true;
      });
    },
  },
  watch: {
    filterText(val) {
      this.$refs.catalogTree.filter(val);
    },
  },
};
</script>

<style lang="scss" scoped>
.image {
  width: 60px;
  height: 60px;
  margin: 0;
  object-fit: cover;
}
.catalog-wrapper {
  background: #fff;
  padding: 20px;
  border-radius: 6px;
  .input-box {
    display: flex;
    .toggle-expand {
      height: 36px;
      margin-left: 5px;
    }
  }
  .search {
    margin-bottom: 10px;
  }
  .catalog-tree {
    height: calc(100vh - 362px);
    width: 100%;
    overflow: auto;
    ::v-deep .el-tree-node {
      padding: 2px 0;
    }
  }
}
::v-deep .catalog-wrapper {
  .el-tree > .el-tree-node {
    display: inline-block;
    min-width: 100%;
    white-space: nowrap;
  }
}
</style>